package com.itlaobing.demo.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.List;
import java.util.Map;

/**
 * @Classname MySessionListener
 * @Description TODO()
 * @Date 2020/11/13 0013 10:27
 * @Author by Administrator
 * @Version v1.0
 */
@WebListener
public class MySessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        // session 销毁的时候, 判断 session 登录过没有.在在线用户session中有就登录过
        HttpSession session = httpSessionEvent.getSession();
        ServletContext context = session.getServletContext();
        // 在线用户
        Map<String, HttpSession> map = (Map<String, HttpSession>) context.getAttribute("onlineUsers");
        map.remove(session.getId());
    }
}
